package com.xinghai.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xinghai.domain.JkqActivity;
import com.xinghai.domain.JkqUser;
import com.xinghai.domain.JkqUserBadge;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.mapper.JkqActivityLogMapper;
import com.xinghai.domain.JkqActivityLog;
import com.xinghai.service.IJkqActivityLogService;

/**
 * 活动记录Service业务层处理
 *
 * @author xinghai
 * @date 2025-08-14
 */
@Service
public class JkqActivityLogServiceImpl extends MPJBaseServiceImpl<JkqActivityLogMapper, JkqActivityLog>
        implements IJkqActivityLogService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询活动记录列表
     *
     * @param jkqActivityLog 活动记录
     * @return 活动记录集合
     */
    @Override
    public List<JkqActivityLog> list(JkqActivityLog jkqActivityLog) {
        MPJLambdaWrapper<JkqActivityLog> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(JkqActivityLog.class);
        wrapper.setAlias("al");
        wrapper.selectAs(JkqActivity::getTitle, JkqActivityLog::getTitle);
        wrapper.selectAs(JkqActivity::getImageUrl, JkqActivityLog::getActivityImageUrl);
        wrapper.selectAs(JkqUser::getHealthUserId, JkqActivityLog::getHealthUserId);
        wrapper.leftJoin(JkqActivity.class,"a", JkqActivity::getId, JkqActivityLog::getActivityId);
        wrapper.leftJoin(JkqUser.class, "u", JkqUser::getId, JkqUserBadge::getUserId);
        if (jkqActivityLog.getActivityId()!= null ){
            wrapper.eq("al.activity_id", jkqActivityLog.getActivityId());
        }
        if (jkqActivityLog.getUserId()!= null ){
            wrapper.eq("al.user_id", jkqActivityLog.getUserId());
        }
        if (jkqActivityLog.getHealthUserId()!= null ){
            wrapper.eq("u.health_user_id", jkqActivityLog.getHealthUserId());
        }
        if (jkqActivityLog.getName()!= null  && !jkqActivityLog.getName().isEmpty()){
            wrapper.like("al.name", jkqActivityLog.getName());
        }
        if (jkqActivityLog.getYqUserId()!= null ){
            wrapper.eq("yq_user_id", jkqActivityLog.getYqUserId());
        }
        if (jkqActivityLog.getType()!= null  && !jkqActivityLog.getType().isEmpty()){
            wrapper.eq("al.type", jkqActivityLog.getType());
        }
        if (jkqActivityLog.getStatus()!= null  && !jkqActivityLog.getStatus().isEmpty()){
            wrapper.eq("al.status", jkqActivityLog.getStatus());
        }
        return this.baseMapper.selectJoinList(JkqActivityLog.class,wrapper);
    }

    /**
     * 导入活动记录信息
     *
     * @param list 活动记录信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JkqActivityLog> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JkqActivityLogMapper jkqActivityLogMapper = sqlSession.getMapper(JkqActivityLogMapper.class);
            list.forEach(jkqActivityLogMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
